package com.rdc.qingkou.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.base.BaseActivity;
import com.rdc.qingkou.contract.LoginContract;
import com.rdc.qingkou.presenter.LoginPrestener;


public class LoginActivity extends BaseActivity<LoginPrestener> implements LoginContract.View {


    private static final String TAG = "LoginActivity";

    private ImageButton mBack;
    private EditText mAccount;
    private EditText mPassword;
    private TextView mForgotPassword;
    private TextView mRegister;
    private Button mLogin;
    private boolean isChangePassword;




    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.windowBackground), true);
        setContentView(R.layout.login);

        initView();


    }


    private void initView() {
        mBack = (ImageButton) findViewById(R.id.ibtn_back);
        mAccount = (EditText) findViewById(R.id.et_account);
        mPassword = (EditText) findViewById(R.id.et_password);
        mForgotPassword = (TextView) findViewById(R.id.tv_forgotPassword);
        mRegister = (TextView) findViewById(R.id.tv_register);
        mLogin = (Button) findViewById(R.id.btn_login);


        //放回上一个界面
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //跳转到注册
        mForgotPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isChangePassword = true;
                mPresenter.jumpToRegister(isChangePassword);
            }
        });


        //跳转到注册
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isChangePassword = false;
                mPresenter.jumpToRegister(isChangePassword);
            }
        });


        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(getAccount())) {
                    Toast.makeText(LoginActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                }

                if (!TextUtils.isEmpty(getAccount()) && TextUtils.isEmpty(getPassword())) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }

                if (!TextUtils.isEmpty(getAccount()) && !TextUtils.isEmpty(getPassword())) {
                    mPresenter.login();
                }
            }
        });
        MyApplication.addActivity(this);


    }


    @Override
    public void showFail(String reason) {
        Toast.makeText(LoginActivity.this,reason, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getPassword() {
        return mPassword.getText().toString();
    }

    @Override
    public String getAccount() {
        return mAccount.getText().toString();
    }

    @Override
    public void jumpToRegister(boolean IsChangePassword) {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        intent.putExtra("isChangepassword",IsChangePassword);
        startActivity(intent);

    }

    @Override
    public void jumpToHomePage() {
        Intent intent = new Intent(LoginActivity.this, HomepageActivity.class);
        startActivity(intent);
        MyApplication.delete();
    }

    @Override
    public LoginPrestener getInstance() {
        return new LoginPrestener();
    }



}
